package com.report.service.impl;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.zhxu.bs.BeanSearcher;
import cn.zhxu.bs.util.MapUtils;
import com.report.dto.response.SearchRes;
import com.report.entity.Search;
import com.report.mapper.MemberMapper;
import com.report.mapper.SearchMapper;
import com.report.service.ISearchService;
import com.zmh.framework.constants.Constants;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author summer
 * @since 2024/06/25 14:55:06
 */
@Service
@Transactional(rollbackFor = Exception.class)
@AllArgsConstructor
public class SearchServiceImpl extends ServiceImpl<SearchMapper, Search> implements ISearchService {

    private final BeanSearcher beanSearcher;

    @Override
    public List<SearchRes> searchList() {
        Map<String, Object> build = MapUtils.builder().page(0, 15).build();
        return beanSearcher.searchList(SearchRes.class, build);
    }

    @Override
    public Map<String, Object> findSearch(Integer memberId) {
        Map<String,Object> map = new HashMap<>();
        LambdaQueryWrapper<Search> eq = Wrappers.lambdaQuery(Search.class).eq(Search::getMemberId, memberId);
        List<Search> list = this.list(eq);
        Page<Search> page = new Page<>(1,10);
        LambdaQueryWrapper<Search> not = Wrappers.lambdaQuery(Search.class).notIn(Search::getMemberId, memberId);
        Page<Search> page1 = this.page(page, not);
        map.put("list", list);
        map.put("lists1",page1.getRecords());
        return map;
    }

    @Override
    public void saveSearch(Search search) {
        if(ObjectUtil.isEmpty(search.getMemberId())){
            Search one=new Search();
            one.setName(search.getName());
            this.save(search);
        }else{
            LambdaQueryWrapper<Search> eq = Wrappers.lambdaQuery(Search.class).eq(Search::getMemberId, search.getMemberId()).eq(Search::getName, search.getName());
            Search one = this.getOne(eq);
            if(ObjectUtil.isEmpty(one)){
                one=new Search();
                one.setMemberId(search.getMemberId());
                one.setName(search.getName());
                this.save(search);
            }
        }


    }

    private final MemberMapper memberMapper;

    @Override
    public List<SearchRes> lists(Integer type) {
        List<SearchRes> list= ListUtil.list(true);
        if(type.equals(Constants.ZERO)){
            list=memberMapper.searchLists();
        }else{
            list=memberMapper.reportLists();
        }
        return list;
    }
}
